package com.vancouver.knows.ui.activity.my;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.vancouver.knows.MainApplication;
import com.vancouver.knows.R;
import com.vancouver.knows.business.BSharedPrefManager;
import com.vancouver.knows.business.bean.UserInfo;
import com.vancouver.knows.ui.base.BaseActivity;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 积分
 * Created by llf on 2016/4/27.
 */
public class MyPointsActivity extends BaseActivity {

    private Context mContext;
    @Bind(R.id.exist_points_val)
    TextView existPointsValTxt;
    @Bind(R.id.common_tips)
    TextView commonTipsTxt;
    @Bind(R.id.point_list)
    RelativeLayout pointsDetailLayout;

    private MyPointsDetailView pointDetailView;
    private BSharedPrefManager sharePrefManager;
    private UserInfo userInfo;
    private String pointsRule;
    private String pointsStore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_points);
        mContext = MyPointsActivity.this;
        setViews();
    }

    private void setViews() {
        Intent intent = getIntent();
        commonTipsTxt.setText(intent.getStringExtra("MY_TITLE"));

        sharePrefManager = ((MainApplication) mContext.getApplicationContext()).getSharedPrefManager();
        pointsRule = sharePrefManager.getPointsPolicy();
        pointsStore = sharePrefManager.getPointsStore();

        existPointsValTxt.setText(Html.fromHtml(mContext.getString(R.string.exist_points_val, sharePrefManager.getTotalPoints())));

        pointDetailView = new MyPointsDetailView(mContext, savePointsDetailToList(), sharePrefManager.getTotalPoints(), pointsStore);
        pointsDetailLayout.addView(pointDetailView);
    }

    private String[] savePointsDetailToList() {
        String[] detailList = new String[7];
        if (sharePrefManager != null && sharePrefManager.getUserId() != null) {
            detailList[0]=sharePrefManager.getPostPoints();
            detailList[1]=sharePrefManager.getCommentPoints();
            detailList[2]=sharePrefManager.getImagePoints();
            detailList[3]=sharePrefManager.getUpvotePoints();
            detailList[4]=sharePrefManager.getPostUpvotePoints();
            detailList[5]=sharePrefManager.getCommentUpvotePoints();
            detailList[6]=sharePrefManager.getRedeemedPoints();
        }

        return detailList;

    }

    @OnClick(R.id.common_back)
    public void onCancelClick() {
        finish();
    }

    @OnClick(R.id.exist_point_layout)
    public void onPointsRuleClick() {
        if (pointsRule != null) {
            Intent intent = new Intent(mContext, MyPointsWebViewActivity.class);
            intent.putExtra("USER_NOTICE", mContext.getString(R.string.point_rule));
            intent.putExtra("POINT_URL", pointsRule);
            startActivity(intent);
        }
    }

    @OnClick(R.id.point_shop)
    public void onPointsShopClick() {
        if (pointsStore != null) {
            Intent intent = new Intent(mContext, MyPointsWebViewActivity.class);
            intent.putExtra("USER_NOTICE", mContext.getString(R.string.points_shop));
            intent.putExtra("POINT_URL", pointsStore);
            startActivity(intent);
        }
    }

}
